
/*=========================================
* Author: Administrator
* DateTime:2017/6/20 19:21:23
* Description:$safeprojectname$
==========================================*/

using System;
using System.Collections.Generic;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using UnityEngine;

namespace OrderSystem
{
    /// <summary>
    /// 服务员中介者类，负责协调WaiterView与系统其他部分的交互
    /// 继承自PureMVC的Mediator类，管理服务员视图的事件处理和通知分发
    /// </summary>
    internal class WaiterMediator : Mediator
    {
        /// <summary>
        /// 服务员代理，用于访问和管理服务员数据
        /// </summary>
        private WaiterProxy waiterProxy = null;
        /// <summary>
        /// 中介者名称常量，用于在PureMVC中标识该中介者
        /// </summary>
        public new const string NAME = "WaiterMediator";
        /// <summary>
        /// 获取视图组件，转换为WaiterView类型
        /// </summary>
        public WaiterView WaiterView
        {
            get { return (WaiterView)base.ViewComponent; }
        }

        /// <summary>
        /// 订单代理，用于访问和管理订单数据
        /// </summary>
        private OrderProxy orderProxy = null;

        /// <summary>
        /// 构造函数，初始化WaiterMediator并订阅视图事件
        /// </summary>
        /// <param name="view">关联的WaiterView实例</param>
        public WaiterMediator(WaiterView view) : base(NAME, view)
        {
            WaiterView.CallWaiter += () => { SendNotification(OrderSystemEvent.CALL_WAITER); };
            WaiterView.Order += data => { SendNotification(OrderSystemEvent.ORDER, data); };
            WaiterView.Pay += () => { SendNotification(OrderSystemEvent.PAY); };
            WaiterView.CallCook += () => { SendNotification(OrderSystemEvent.CALL_COOK); };
            WaiterView.ServerFood += item =>
            {
                SendNotification(OrderSystemEvent.selectWaiter, item, "WANSHI"); //付完款之和将服务员状态变更
            };
        }

        /// <summary>
        /// 注册时调用，获取代理实例并初始化视图
        /// </summary>
        public override void OnRegister()
        {
            base.OnRegister();
            waiterProxy = Facade.RetrieveProxy(WaiterProxy.NAME) as WaiterProxy;
            orderProxy = Facade.RetrieveProxy(OrderProxy.NAME) as OrderProxy;
            if (null == waiterProxy)
                throw new Exception(WaiterProxy.NAME + "is null,please check it!");
            if (null == orderProxy)
                throw new Exception(OrderProxy.NAME + "is null,please check it!");
            WaiterView.UpdateWaiter(waiterProxy.Waiters);
        }

        /// <summary>
        /// 定义中介者感兴趣的通知列表
        /// </summary>
        /// <returns>包含通知名称的列表</returns>
        public override IList<string> ListNotificationInterests()
        {
            IList<string> notifications = new List<string>();
            notifications.Add(OrderSystemEvent.CALL_WAITER);
            notifications.Add(OrderSystemEvent.ORDER);
            notifications.Add(OrderSystemEvent.GET_PAY);
            notifications.Add(OrderSystemEvent.FOOD_TO_CLIENT);
            notifications.Add(OrderSystemEvent.ResfrshWarite);
            return notifications;
        }
        /// <summary>
        /// 处理接收到的通知，根据通知类型执行相应操作
        /// </summary>
        /// <param name="notification">接收到的通知对象</param>
        public override void HandleNotification(INotification notification)
        {
            switch (notification.Name)
            {
                case OrderSystemEvent.CALL_WAITER:
                    ClientItem client = notification.Body as ClientItem;
                    SendNotification(OrderSystemEvent.GET_ORDER, client, "Get");//请求获取菜单的命令 GetAndExitOrderCommed
                    break;
                case OrderSystemEvent.ORDER:
                    SendNotification(OrderSystemEvent.CALL_COOK, notification.Body);
                    break;
                case OrderSystemEvent.GET_PAY:
                    ClientItem item = notification.Body as ClientItem;
                   
                    SendNotification(OrderSystemEvent.GUEST_BE_AWAY, item, "Remove");
                    break;
                case OrderSystemEvent.FOOD_TO_CLIENT:
                    // Debug.Log(notification.Body.GetType());
                    WaiterItem waiterItem = notification.Body as WaiterItem;
                    waiterItem.order.client.state++;
                    //  SendNotification(OrderSystemEvent.REST, waiterItem);
                    SendNotification(OrderSystemEvent.PAY, waiterItem);
                    break;
                case OrderSystemEvent.ResfrshWarite:
                    waiterProxy = Facade.RetrieveProxy(WaiterProxy.NAME) as WaiterProxy;
                    WaiterView.Move(waiterProxy.Waiters);//刷新一下服务员的状态
                    break;
            }
        }

    }
}